<?php
require_once('../../includes/classes/core.php');

$action	= $_REQUEST['act'];
$error	= '';
$data	= '';

switch ($action) {
	case 'get_add_page':
		$page		= GetPage();
		$data		= array('page'	=> $page);

		break;
	case 'get_edit_page':
		$comment_id	 = $_REQUEST['id'];
		$page		 = GetPage(Getcomment($comment_id));
		$data		 = array('page'	=> $page);

		break;
	case 'get_list' :
		$count	= $_REQUEST['count'];
		$hidden	= $_REQUEST['hidden'];
			
		$rResult = mysql_query("SELECT 	comment.id,
										comment.`comment`
							    FROM 	comment
							    WHERE 	comment.actived=1");

		$data = array(
				"aaData"	=> array()
		);

		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				/* General output */
				$row[] = $aRow[$i];
				if($i == ($count - 1)){
					$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
				}
			}
			$data['aaData'][] = $row;
		}

		break;
	case 'save_priority':
		$comment_id = $_REQUEST['id'];
		$comment    = $_REQUEST['comment'];
			
		if($comment != ''){
			if(!CheckcommentExist($comment, $comment_id)){
				if ($comment_id == '') {
					Addcomment( $comment_id, $comment);
				}else {
					Savecomment($comment_id, $comment);
				}				
			}else{
				$error = '"' . $comment . '" უკვე არის სიაში!';	
			}
		}
		

		break;
	case 'disable':
		$comment_id	= $_REQUEST['id'];
		Disablecomment($comment_id);

		break;
	default:
		$error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	Category Functions
* ******************************
*/

function Addcomment($comment_id, $comment)
{
	$user_id	= $_SESSION['USERID'];
	mysql_query("INSERT INTO  `comment`
							  (`user_id`,`comment`)
					VALUES 	  
							  ('$user_id','$comment')");

}

function Savecomment($comment_id, $comment)
{
	$user_id	= $_SESSION['USERID'];
	mysql_query("	UPDATE `comment`
					SET    `user_id` ='$user_id',
						   `comment` ='$comment'
					WHERE  `id`      = $comment_id");
}

function Disablecomment($comment_id)
{
	mysql_query("	UPDATE `comment`
					SET    `actived` = 0
					WHERE  `id` = $comment_id");
	
}

function CheckcommentExist($comment)
{
	$res = mysql_fetch_assoc(mysql_query("	SELECT `id`
											FROM   `comment`
											WHERE  `comment` = '$comment' && `actived` = 1"));
	if($res['id'] != ''){
		return true;
	}
	return false;
}


function Getcomment($comment_id)
{
	$res = mysql_fetch_assoc(mysql_query("	SELECT  `id`,
													`comment`
											FROM    `comment`
											WHERE   `id` = $comment_id" ));

	return $res;
}

function GetPage($res = '')
{
	$data = '
	<div id="dialog-form">
	    <fieldset>
	    	<legend>ძირითადი ინფორმაცია</legend>

	    	<table class="dialog-form-table">
				
				<tr>
					
					<td style="width: 170px;"><label for="CallType">კომენტარის შინაარში</label></td>
				</tr>
				<tr>
					
					<td colspan="6">
						<div id="content">	
						<textarea  style="width: 550px; resize: none;" id="comment" class="idle" name="call_content" cols="300" rows="8">' . $res['comment'] . '</textarea>
						</div>		
					</td>
					
				</tr>

			</table>
			<!-- ID -->
			<input type="hidden" id="comment_id" value="' . $res['id'] . '" />
        </fieldset>
	

    </div>
					
    ';
	return $data;
}

?>
